package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.BudgetDto;
import com.ruoyi.system.service.BudgetService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.BudgetAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/budget")
public class BudgetController extends BaseController {
    @Autowired
    private BudgetService service;

    /**
     * 预算科目列表
     * @param budget
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("budget:list")
    public TableDataInfo list(BudgetDto budget){
        startPage();
        List<BudgetDto> budgetDtos = service.selectBlist(budget);
        return getDataTable(budgetDtos);
    }

    /**
     * 导出
     * @param response
     * @param budgetDto
     */
    @PostMapping("/export")
    @RequiresPermissions("budget:export")
    public void export(HttpServletResponse response, BudgetDto budgetDto)
    {
        List<BudgetDto> list = service.selectBlist(budgetDto);
        ExcelUtil<BudgetDto> util = new ExcelUtil(BudgetDto.class);
        util.exportExcel(response, list, "预算科目");
    }

    /**
     * 导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @RequiresPermissions("budget:import")
    @Log(businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BudgetAccount> util = new ExcelUtil(BudgetAccount.class);
        List<BudgetAccount> budList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = service.importBud(budList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 保存或修改
     * @param budgetAccount
     * @return
     * @throws ParseException
     */
    @PostMapping("/save")
    @Log(title = "预算科目",businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody BudgetAccount budgetAccount) throws ParseException {
        boolean b = service.saveOrUpdate(budgetAccount);
        return AjaxResult.success(b);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable Integer id){
        BudgetAccount byId = service.getById(id);
        return AjaxResult.success(byId);
    }
}
